package com.lepu.app.fun.grow;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.application.MyApplication;
import com.app.config.UserConfig;
import com.app.utils.Const;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.PopUtil;
import com.core.lib.utils.main.UIHelper;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.bean.PhotoItem;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;

public class GrowCloudUploadActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, OnTopbarNewRightButtonListener {

	private CustomTopBarNew mTopbar;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grow_cloud_upload_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {

		// topbar
		mTopbar = (CustomTopBarNew) findViewById(R.id.topbar);
		mTopbar.setTopbarTitle("云端同步");
		mTopbar.setonTopbarNewLeftLayoutListener(this);
		// mTopbar.setOnTopbarNewRightButtonListener(this);

		// updateCloudTurnState();

		String uid = MyApplication.getInstance().getLoginInfo().UserID;
		ArrayList<PhotoItem> dataList = PhotoItem.getAllPhotos(uid);
		int allCount = dataList != null ? dataList.size() : 0;
		
		ArrayList<PhotoItem> noUploadList = PhotoItem.getAllPhotosNoUpdate(uid);
		final int noUploadCount = noUploadList != null ? noUploadList.size() : 0;

		Button uploadButton = (Button) findViewById(R.id.uploadButton);
		uploadButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				if (noUploadCount > 0) {
					if (GrowImageManager.getInstance().mIsUploading
							|| GrowApplyPhotoManager.getInstance().mIsUploading) {
						UIHelper.showToast(GrowCloudUploadActivity.this,
								"您正在上传...");
						return ;
					} else {
						// 保存到云端
						GrowImageManager.getInstance().init();
						GrowImageManager.getInstance().uploadImageContent();
						
						finish(true);
						UIHelper.showToast(GrowCloudUploadActivity.this,
								"云端同步将在后台进行");
					}
				} else {
					UIHelper.showToast(MyApplication.getInstance(),
							"当前没有可同步的照片");
				}
			}
		});

		TextView allRecordTextView = (TextView) findViewById(R.id.allRecordTextView);
		TextView uploadTextView = (TextView) findViewById(R.id.uploadTextView);
		TextView dowloadTextView = (TextView) findViewById(R.id.dowloadTextView);

		allRecordTextView.setText(String.format("共有%d条数据,其中%d条待同步", allCount,
				noUploadCount));
		uploadTextView
				.setText(String.format("上传%d条", allCount - noUploadCount));
		dowloadTextView.setText(String.format("下载%d条", 0));
	}

	// private void updateCloudTurnState() {
	// boolean flag = AppConfig.getConfigBoolean(this,
	// Const.CONFIG_APP_GROW_CLOUD_TURN_STATE, true);
	// if (flag) {
	// mTopbar.setRightButton(R.drawable.grow_cloud_upload_turn_on);
	// } else {
	// mTopbar.setRightButton(R.drawable.grow_cloud_upload_turn_off);
	// }
	// }

	@SuppressLint("InflateParams")
	private void showPopMainRight(View v) {
		View view = LayoutInflater.from(this).inflate(
				R.layout.grow_cloud_pop_main_right, null);
		LinearLayout popItemLayoutTurnOn = (LinearLayout) view
				.findViewById(R.id.popItemLayoutTurnOn);
		LinearLayout popItemLayoutTurnOff = (LinearLayout) view
				.findViewById(R.id.popItemLayoutTurnOff);

		popItemLayoutTurnOn.setOnClickListener(mPopRightClickListener);
		popItemLayoutTurnOff.setOnClickListener(mPopRightClickListener);

		PopUtil.getInstance().showPopWindow(this, v, view, 0, null, true);
	}

	private View.OnClickListener mPopRightClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			int rid = v.getId();
			switch (rid) {
			case R.id.popItemLayoutTurnOn:

				UserConfig.setConfig(getApplicationContext(),
						Const.CONFIG_APP_GROW_CLOUD_TURN_STATE, true);
				// updateCloudTurnState();
				break;
			case R.id.popItemLayoutTurnOff:

				UserConfig.setConfig(getApplicationContext(),
						Const.CONFIG_APP_GROW_CLOUD_TURN_STATE, false);
				// updateCloudTurnState();
				break;

			default:
				break;
			}

			PopUtil.getInstance().dismissPopWindow();
		}
	};

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {

		ImageButton rightButton = (ImageButton) mTopbar
				.findViewById(R.id.topbarRightImageButton);
		showPopMainRight(rightButton);
	}
}
